import { defineStore } from 'pinia'

import { getConfig } from '@/api/app'

interface AppSate {
  config: Record<string, any>
  isMobile: boolean
  isCollapsed: boolean
  isRouteShow: boolean
}

const useAppStore = defineStore('app', {
  state: (): AppSate => {
    return {
      config: {},
      isMobile: true,
      isCollapsed: false,
      isRouteShow: true,
    }
  },
  actions: {
    getImageUrl(url: string) {
      return url.indexOf('http') ? `${this.config.oss_domain}${url}` : url
    },
    getConfig() {
      return new Promise((resolve, reject) => {
        const data = {
          oss_domain: 'https://php.likeadmin.cn/',
          web_name: 'Think FastAPI',
          web_favicon: 'https://php.likeadmin.cn/resource/image/adminapi/default/web_favicon.ico',
          web_logo: 'https://php.likeadmin.cn/resource/image/adminapi/default/web_logo.jpg',
          login_image: 'https://php.likeadmin.cn/resource/image/adminapi/default/login_image.png',
          copyright_config: [
            {
              key: '粤ICP备19081985号',
              value: 'https://beian.miit.gov.cn',
            },
          ],
          version: '1.9.4',
        }
        this.config = data
        resolve(data)
        // getConfig()
        //     .then((data) => {
        //         this.config = data
        //         resolve(data)
        //     })
        //     .catch((err) => {
        //         reject(err)
        //     })
      })
    },
    setMobile(value: boolean) {
      this.isMobile = value
    },
    toggleCollapsed(toggle?: boolean) {
      this.isCollapsed = toggle ?? !this.isCollapsed
    },
    refreshView() {
      this.isRouteShow = false
      nextTick(() => {
        this.isRouteShow = true
      })
    },
  },
})

export default useAppStore
